import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:palmf_hid/common/utils/my_log_util.dart';
import 'package:palmf_hid/common/values/my_color.dart';
import 'package:palmf_hid/generated/assets.dart';
import 'package:palmf_hid/pages/main/check/check_view.dart';
import 'package:palmf_hid/pages/main/disposition/disposition_view.dart';
import 'package:palmf_hid/pages/main/history/history_view.dart';
import 'package:palmf_hid/pages/main/patient/patient_view.dart';

import 'home_logic.dart';
import 'home_state.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final HomeLogic logic = Get.put(HomeLogic());
  final HomeState state = Get.find<HomeLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: MyColor.account_color,
      body: Obx(() => _buildBody()),
    );
  }

  _buildBody() {
    return SizedBox(
      width: 1.sw,
      height: 1.sh,
      child: Column(
        children: [_buildTitle(), Expanded(child: _buildHome())],
      ),
    );
  }

  _buildHome() {
    return Row(children: [
      NavigationRail(
        labelType: state.isExpand
            ? NavigationRailLabelType.none
            : NavigationRailLabelType.all,
        minExtendedWidth: 0.15.sw,
        minWidth: 0.08.sw,
        selectedLabelTextStyle: const TextStyle(color: MyColor.account_color),
        leading: InkWell(
            child: const Icon(
              Icons.menu_open,
              color: Colors.grey,
            ),
            onTap: () {
              state.isExpand = !state.isExpand;
            }),
        extended: state.isExpand,
        trailing: Expanded(
          child: Align(
            alignment: Alignment.bottomCenter,
            child: Padding(
              padding: const EdgeInsets.only(bottom: 20.0),
              child: Image.asset(
                Assets.imagesLogoXLeisen,
                width: 0.02.sw,
                fit: BoxFit.scaleDown,
              ),
            ),
          ),
        ),
        useIndicator: false,
        onDestinationSelected: _onDestinationSelected,
        destinations: [
          NavigationRailDestination(
              icon: Image.asset(
                state.index == 0 ? Assets.imagesTestActive : Assets.imagesTest,
                width: 0.03.sw,
                fit: BoxFit.scaleDown,
              ),
              label: const Text("检测")),
          NavigationRailDestination(
              icon: Image.asset(
                state.index == 1
                    ? Assets.imagesTest2Active
                    : Assets.imagesTest2,
                width: 0.03.sw,
                fit: BoxFit.scaleDown,
              ),
              label: const Text("配置")),
          NavigationRailDestination(
              icon: Image.asset(
                state.index == 2
                    ? Assets.imagesTest3Active
                    : Assets.imagesTest3,
                width: 0.03.sw,
                fit: BoxFit.scaleDown,
              ),
              label: const Text("患者")),
          NavigationRailDestination(
              icon: Image.asset(
                state.index == 3
                    ? Assets.imagesTest4Active
                    : Assets.imagesTest4,
                width: 0.03.sw,
                fit: BoxFit.scaleDown,
              ),
              label: const Text("查询")),
          NavigationRailDestination(
              icon: Image.asset(
                state.index == 5
                    ? Assets.imagesTest6Active
                    : Assets.imagesTest6,
                width: 0.03.sw,
                fit: BoxFit.scaleDown,
              ),
              label: const Text("关闭"))
        ],
        selectedIndex: state.index,
      ),
      const SizedBox(
        width: 10,
      ),
      Expanded(
          child: PageView(controller: logic.pageController, children: const [
        CheckPage(),
        DispositionPage(),
        PatientPage(),
        HistoryPage()
      ]))
    ]);
  }

  void _onDestinationSelected(int value) {
    state.index = value;
    if (value == 4) {
      exit(0);
    }
    logic.pageController.jumpToPage(value);
  }

  _buildTitle() {
    return Obx(() => Container(
        padding: EdgeInsets.symmetric(horizontal: 0.01.sw),
        width: 1.sw,
        height: 0.08.sh,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            const Text(
              "掌中快检平台",
              style: TextStyle(
                  color: MyColor.c_FFFFFFFF,
                  fontSize: 18,
                  fontWeight: FontWeight.bold),
            ),
            Text(state.syncNum),
            _buildState()
          ],
        )));
  }

  _buildState() {
    return Row(children: [
      Icon(
        Icons.circle,
        size: 20,
        color: state.status == 0 ? Colors.red : Colors.green,
      ),
      const SizedBox(
        width: 10,
      ),
      InkWell(
        child: Container(
          padding: const EdgeInsets.only(right: 15),
          child: const Icon(
            Icons.settings,
            size: 25,
            color: Colors.white,
          ),
        ),
        onTap: () {},
      )
    ]);
  }

  @override
  void dispose() {
    Get.delete<HomeLogic>();
    super.dispose();
  }
}
